import {isDecimal, isPhoneNum} from '@/utils/tools.js'
let vm = null
export default {
  name: "validata", 
  data() {
    return {};
  },
  mounted(){
    vm = this
  },
  methods: {
    isError(str){
      if(str === '') return true
      return false
    },
    // 服务内容
    validateFwnr: (rule, value, callback) => {
      let {serveTypeIds = '', activityTypeId} = vm.currentItem
      let len = vm.tableData.length
      if(!serveTypeIds.length){
        vm.$message.error('选择服务内容')
        callback(new Error('选择服务内容'));
      }else{        
        if(activityTypeId == 7){  
          let total = 0
          vm.tableData.forEach(i => {
            total += parseFloat(i.probability || '0')
          })
          if(len != 6){
            vm.$message.error('请设置6个奖项')
            callback(new Error('请设置6个奖项'));
          }else if(total !== 1){
            vm.$message.error('奖项之和须是100%')
            callback(new Error('奖项之和须是100%'));
          }else{
            callback();
          }
        }else{
          callback();
        }        
      }
    },
    // 活动份数
    validateHdfs: (rule, value, callback) => {
      let reg = /^\d+$/
      if (!reg.test(value)) {
        vm.$message.error('请输入正确的整数')
        callback(new Error('请输入正确的整数'));
      } else {
        callback();
      }
    },
    // 活动时间
    validateHdSJ: (rule, value, callback) => {      
      let {startingTime, endTime} = vm.currentItem
      if (!startingTime || startingTime == '') {
        vm.$message.error('请选择活动开始时间')
        return callback(new Error('请选择活动开始时间'));
      }
      
      if (!endTime || endTime == '') {
        vm.$message.error('请选择活动结束时间')
        return callback(new Error('请选择活动结束时间'));
      }
      if(new Date(startingTime).getTime() >= new Date(endTime).getTime()) {
        vm.$message.error('开始时间应小于结束时间')
        return callback(new Error('开始时间应小于结束时间'));
      }
      callback();
    },
    // 卡券有效期
    validateKqyxq: (rule, value, callback) => {     
      let {closingDate, whetherToClosingDate, endTime} = vm.currentItem
      if (!closingDate && !whetherToClosingDate) {
        vm.$message.error('请设置卡券有效期')
        return callback(new Error('请设置卡券有效期'));
      }
      if(closingDate){
        whetherToClosingDate = false
        if(new Date(closingDate).getTime() < new Date(endTime).getTime()){
          vm.$message.error('卡券有效期应大于等于结束时间')
          return callback(new Error('卡券有效期应大于等于结束时间'));
        }
      }
      if(whetherToClosingDate){
        closingDate = ''
      }
      callback();
    },
    // 购买限制
    validateGmxz: (rule, value, callback) => {      
      let {limitation, emptionRestrict} = vm.currentItem
      let reg = /^\d+$/
      if(!limitation){
        emptionRestrict = ''
        callback();
      }else{
        if (!emptionRestrict || emptionRestrict == '') {
          vm.$message.error('请设置限制具体数目')
          return callback(new Error('请设置限制具体数目'));
        }
        if (!reg.test(emptionRestrict)) {
          vm.$message.error('请输入正确的整数')
          return callback(new Error('请输入正确的整数'));
        }
        callback();
      }
    },
    // 分享红包
    validateFxhb: (rule, value, callback) => {      
      let {WhetherToShare, completeIncome} = vm.currentItem
      if (WhetherToShare && (!isDecimal(`${completeIncome}`))) {
        vm.$message.error('请设置正确的分享金额')
        return callback(new Error('请设置正确的分享金额'));
      }
      callback();
    },
    // 激活选项
    validateJhxx: (rule, value, callback) => {      
      let {shopActivation, activationClosingDate, endTime} = vm.currentItem
      if (vm.isError(shopActivation)) {
        vm.$message.error('请设置是否到店激活')
        return callback(new Error('请设置是否到店激活'));
      }
      if (vm.isError(activationClosingDate)) {
        vm.$message.error('请设置激活截止日期')
        return callback(new Error('请设置激活截止日期'));
      }
      if(new Date(activationClosingDate).getTime() > new Date(endTime).getTime()) {
        vm.$message.error('激活截止日期应小于活动结束时间')
        return callback(new Error('激活截止日期应小于活动结束时间'));
      }
      callback();
    },
    // 技术服务费
    validateJsfwf: (rule, value, callback) => {      
      let {skillServeFee} = vm.currentItem
      if (vm.isError(skillServeFee)) {
        vm.$message.error('请设置技术服务费付费方式')
        return callback(new Error('请设置技术服务费付费方式'));
      }
      callback();
    },
    // 适用商户
    validateSysh: (rule, value, callback) => {      
      let {shopPool, suitShopIds} = vm.currentItem
      if (vm.isError(shopPool)) {
        vm.$message.error('请设置适用商户池')
        return callback(new Error('请设置适用商户池'));
      }
      if (vm.isError(suitShopIds)) {
        vm.$message.error('请选择适用商户')
        return callback(new Error('请选择适用商户'));
      }
      callback();
    },
    // 成团条件
    validateCttj: (rule, value, callback) => {      
      let {clusteringCondition, acddGroupBuyings = [], groupBuyingPrice} = vm.currentItem
      let reg = /^\d+$/
      if (vm.isError(clusteringCondition)) {
        vm.$message.error('请设置成团条件')
        return callback(new Error('请设置成团条件'));
      }
      if (clusteringCondition == '0'){
        if(!acddGroupBuyings.length){
          vm.$message.error('请设置阶梯团购价')
          return callback(new Error('请设置阶梯团购价'));
        }else{
          // acddGroupBuyings = [{miniValue: '', maxValue: '', price: ''}]
          let idx = acddGroupBuyings.findIndex(i => (!reg.test(i.miniValue)) || (!reg.test(i.maxValue)) || (!isDecimal(`${i.price}`)))
          if(idx != -1) {
            vm.$message.error('请设置正确的阶梯团购价')
            return callback(new Error('请设置正确的阶梯团购价'))
          }
        }
      } 
      if (clusteringCondition == '1' && (!isDecimal(`${groupBuyingPrice}`))) {
        vm.$message.error('请设置正确的团购统一价')
        return callback(new Error('请设置正确的团购统一价'));
      }
      callback();
    },
    // 定向用户
    validateDxyh: (rule, value, callback) => {      
      let {acddUsers, activityTypeId} = vm.currentItem, isError = false
      if (!acddUsers || !acddUsers.length) {
        vm.$message.error('请设置定向用户')
        return callback(new Error('请设置定向用户'));
      }
      acddUsers.forEach(i => {
        if(!i.name || i.name == '') {
          isError = true
          vm.$set(i, 'name_error', true)
        }
        if(!i.phone || i.phone == '') {
          isError = true
          vm.$set(i, 'phone_error', true)
        }
      })
      if(isError) {
        vm.$message.error('请完善用户配置')
        return callback('请完善用户配置')
      }
      acddUsers.forEach(i => {
        delete i['name_error'] 
        delete i['phone_error']
      })
      callback();
    },
    validateMoney: (rule, value, callback) => {
      let {currentItem} = vm
      if (!isDecimal(`${value}`)) {
        vm.$message.error('请输入正确的金额')
        callback(new Error('请输入正确的金额'));
      } else {
        callback();
      }
    },
    rowMoneyChange(e, row, key){
      let _self = this
      let {innerText} = e.target
      let boo = isDecimal(innerText)
      if(!boo) return _self.$set(row, `${key}_error`, true)
      _self.$set(row, `${key}_error`, false)
      _self.$set(row, key, innerText)
    },
    rowTelChange(e, row, key){
      let _self = this
      let {innerText} = e.target
      let boo = isPhoneNum(innerText)
      if(!boo) return _self.$set(row, `${key}_error`, true)
      _self.$set(row, `${key}_error`, false)
      _self.$set(row, key, innerText)
    },
    rowTxtChange(e, row, key){
      let _self = this
      let {innerText} = e.target
      if(!key){
        _self.$set(row, key, innerText)
      }else{
        if(!innerText || innerText == '') return _self.$set(row, `${key}_error`, true)
        _self.$set(row, `${key}_error`, false)
        _self.$set(row, key, innerText)
      }      
    }
  }
};